<template>
  <div class="login_container">
    <div class="title-container">
      <h3 class="title">MyJob</h3>
    </div>
    <el-card class="box-card">
      <el-form ref="form" :model="loginForm" label-width="80px">
        <el-form-item label="用户名:">
          <el-input v-model="loginForm.username"></el-input>
        </el-form-item>
        <el-form-item label="密码:">
          <el-input type="password" v-model="loginForm.password"></el-input>
        </el-form-item>
        <el-form-item label="身份:">
          <el-radio-group v-model="loginForm.role">
            <el-radio label="1">学生</el-radio>
            <el-radio label="2">招聘方</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item>
          <el-button @click="registHandle">立即注册</el-button>
          <el-button type="primary" @click="LoginHandle">登&nbsp;&nbsp;&nbsp;&nbsp;录</el-button>
        </el-form-item>
        <el-link type="primary">忘记密码?</el-link>
      </el-form>
    </el-card>
  </div>
</template>

<script>
import { studentLoginAPI } from '@/api/studentAPI/studentAPI.js'
import { recruiterLoginAPI } from '@/api/recruiterAPI/recruiterAPI.js'
export default {
  name: 'login',
  data() {
    return {
      loginForm: {
        username: '',
        password: '',
        role: '1'
      },
      loading: false,
      pwdType: 'password'
    }
  },
  methods: {
    //登录方法
    async LoginHandle() {
      // role === 1学生登录
      if (this.loginForm.role === '1') {
        // 1 发出请求 /login/?username=xxx&&password=xxx&&role=xxx
        const res = await studentLoginAPI(this.loginForm.username, this.loginForm.password)
        if (res.data.msg) {
          //res.data.msg不为null时,内容为返回的错误信息
          var _msg = res.data.msg
          this.$message.error(_msg)
        } else {
          //res.data.msg为null,表示登录成功
          // this.$message.success('登录成功!')
          //跳转到主页
          this.$router.push('/home')
        }
      } else {
        //role === 2 招聘方登录
        //  发出请求
        const res = await recruiterLoginAPI(this.loginForm.username, this.loginForm.password)
        if (res.data.msg) {
          //res.data.msg不为null时,内容为返回的错误信息
          var _msg = res.data.msg
          this.$message.error(_msg)
        } else {
          //跳转到主页
          this.$router.push('/home')
        }
      }
    },
    registHandle() {
      this.$router.push('/regist')
    }
  }
}
</script>

<style lang="less" scoped>
.text {
  font-size: 14px;
}

.item {
  padding: 18px 0;
}

.box-card {
  margin: 30px auto;
  width: 480px;
}

.title-container {
  position: relative;

  .title {
    font-size: 30px;
    // color: #eee;
    margin: 50px auto 40px auto;
    text-align: center;
    font-weight: 500;
  }
}
.el-button + .el-button {
  margin-left: 71px;
}
/deep/ .el-button--primary {
  padding: 13px 50px;
}
</style>
